from pip._internal import main as pip

pip(['install','opencv-python', 'onnxruntime-gpu', 'requests', 'tkinterx', '--quiet'])


import cv2
import numpy as np
import tkinter as tk
import Auxiliary
from Recognition import Recognition
from Camera import Camera

point = (320, 240)
frame_shape = (640, 480)

def mouse_callback(event, x, y, flags, param):
    global point
    if event == cv2.EVENT_LBUTTONUP:
        point = (int(x / frame_shape[0] * 640), int(y / frame_shape[1] * 640))

if __name__ == "__main__":
	recognition = Recognition('yolov8n-pose.onnx')
	camera = Camera('http://192.168.137.137')
	camera.SetSize(8)
	camera.SetQuality(4)
	camera.SetBrightness(0.6)
	camera.SetContrast(2.0)
	camera.SetSaturation(1.4)
	camera.Start()
	cv2.namedWindow("before")
	cv2.namedWindow("after")
	cv2.setMouseCallback("after", mouse_callback)
	w1 = Auxiliary.configWin(camera)
	while True:
		recognition.point = point
		frame = camera.GetFrame()
		if frame is None:
			continue

		cv2.imshow("before", frame)
		frame_shape = frame.shape[1::-1]
		result, pic = recognition.GetResult(frame)
		cv2.imshow('after', pic)
		if not camera.stream_normal:
			Auxiliary.debugLog("Stream is not normal!")
		# Auxiliary.debugLog(str(result))
		if cv2.waitKey(1) == ord('q'):
			break
	camera.Stop()